﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TWS.Cart.Business;
using TWS.Cart.Data;

public partial class Application_Controls_Cart : System.Web.UI.UserControl
{
    ShoppingCartManager scm = new ShoppingCartManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RepeaterCart.DataSource = scm.LstItems;
            RepeaterCart.DataBind();			
        }

		//Para el label de no products
		if (scm.LstItems.Count > 0)
			LiteralNoRecords.Visible = false;
		else
			LiteralNoRecords.Visible = true;

		//For the Login and new user pane.
		if (SecurityContext.GetUser() != null)
			Fieldset01.Visible = false;

    	CalculateTotal();
    }

	public void CalculateTotal()
	{
		if (scm.LstItems.Count > 0)
		{
			LiteralShippingCompany.Text = scm.GetShippingQuote().Company;
			LiteralShippingUnitCost.Text = string.Format("{0:c}", scm.GetShippingQuote().Cost);
			LiteralShippingSubTotal.Text = string.Format("{0:c}", scm.GetShippingQuote().Cost);

			LiteralTotal.Text = string.Format("{0:c}", (scm.GetShippingQuote().Cost + scm.GetOrderTotal()));
		}
	}

	public void bDel_Click(object sender, EventArgs e)
    {
        int ItemIndex = Convert.ToInt32(((Button)sender).Attributes["vID"]);
        scm.LstItems.RemoveAt(ItemIndex);

        RepeaterCart.DataSource = scm.LstItems;
        RepeaterCart.DataBind();

    	MessageBox01.Visible = true;
    	MessageBox01.Text = Resources.Resource.ProductDeletedFromCart;

		CalculateTotal();
    }

	public void LoggedIn_Occured(object sender, EventArgs e)
	{
		CartCatConfigurationParameter ssConfigParam = new CartCatConfigurationParameter(CartCatConfigurationParameter.Columns.KeyName, "ConfirmUrl");
		Response.Redirect(ssConfigParam.ValueX);
	}

	public void ButtonUpdateCart_Click(object sender, EventArgs e)
	{
		
		foreach (RepeaterItem repeaterItem in RepeaterCart.Items)
		{
			if (repeaterItem.ItemType == ListItemType.Item | repeaterItem.ItemType == ListItemType.AlternatingItem)
			{
				TextBox tb = (TextBox)repeaterItem.FindControl("TextBoxQuantity");
				scm.LstItems[repeaterItem.ItemIndex].Quantity = Convert.ToInt32(tb.Text);
			}
		}

		RepeaterCart.DataSource = scm.LstItems;
		RepeaterCart.DataBind();

		MessageBox01.Visible = true;
		MessageBox01.Text = Resources.Resource.ProductUpdatedFromCart;

		CalculateTotal();
	}			

	public void ButtonConfirmOrder_Click(object sender, EventArgs e)
	{
		try
		{
			CartTranOrder ssCTO = new CartTranOrder();
			ssCTO.DateOfOrder = DateTime.Now;
			ssCTO.ClientsId = SecurityContext.GetUser().Id;
			ssCTO.SubTotal = scm.GetOrderSubTotal();
			ssCTO.Vat = 0;
			ssCTO.Total = ssCTO.SubTotal + ssCTO.Vat;
			ssCTO.OrderStatusId = (int)OrderStatus.Pedido;
			ssCTO.PaymentTypeId = (int)PaymentType.Deposito;
			ssCTO.Save();

			foreach(var oItem in scm.LstItems)
			{
				CartTranOrderDetail ssCTOD = new CartTranOrderDetail();
				ssCTOD.OrdersId=ssCTO.Id;
				ssCTOD.ProductsId=oItem.Id;
				ssCTOD.SalePrice=oItem.SubTotal;
				ssCTOD.Quantity=oItem.Quantity;
				ssCTOD.Save();

				foreach(var attribItem in oItem.AdditionalAttributes)
				{
					CartRelOrderDetailsAttribute ssCRODA = new CartRelOrderDetailsAttribute();
					ssCRODA.OrderDetailsId=ssCTOD.Id;
					ssCRODA.AttributesId=attribItem.Id;
					ssCRODA.ValueX = attribItem.Value;
					ssCRODA.Save();					
				}

			}


		}
		catch (Exception ex)
		{
			Response.Write(ex.Message + "<br>" + ex.ToString());
			
		}

		MessageBox01.Visible = true;
		MessageBox01.Text = Resources.Resource.OrderPlaced;
	}
}
